package com.android.settings.ios;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

public class OsListFragment extends Fragment {

    private int mLayoutId = R.layout.os_list_view_fragment;
    private ListView mListView;

    private String mMainTitle;
    private String mBackSubTitle;

    public void setContentView(int res) {
        mLayoutId = res;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBackSubTitle = bundle.getString("fragment_back", null);
            mMainTitle = bundle.getString("fragment_title", getString(R.string.app_name));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mLayoutId != 0) {
            View view = inflater.inflate(mLayoutId, null);
            mListView = view.findViewById(android.R.id.list);
            onViewInflated(view, mListView);
            if (mListView != null) {
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        onListItemClick(parent, view, position, id);
                    }
                });
            }
            return view;
        }
        return null;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateTitleIfNeeded();
    }

    public ListView getListView() {
        return mListView;
    }

    protected void updateTitleIfNeeded() {
        Activity activity = getActivity();
        if (activity instanceof SettingsMainActivity) {
            activity.setTitle(mMainTitle);
            ((SettingsMainActivity) activity).setLeftBackText(mBackSubTitle);
        }
    }

    public void entryNewScreen(Fragment fragment) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            activity.getSupportFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit, R.anim.fragment_pop_enter, R.anim.fragment_pop_exit)
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        }
    }

    public void entryNewScreen(String title, String fragmentPath) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            final Bundle args = new Bundle();
            final Fragment fragment = fragmentManager.getFragmentFactory().instantiate(requireActivity().getClassLoader(), fragmentPath);
            args.putString("fragment_back", mMainTitle);
            args.putString("fragment_title", title);
            fragment.setArguments(args);
            entryNewScreen(fragment);
        }
    }

    protected void onViewInflated(View view, ListView listView) {
        // Add other view.
    }

    protected void onListItemClick(AdapterView<?> parent, View v, int position, long id) {

    }
}
